package stu.ais.HomeWork;

import java.util.Arrays;

public class Employee {
    private int id;

    private String name;

    private double salary;

    //自关联的典型的案例
    //一个上司是对应多个下属的
    private Employee[] employees;

    public Employee(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Employee[] getEmployees() {
        return employees;
    }

    public void setEmployees(Employee[] employees) {
        this.employees = employees;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Employee{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", salary=").append(salary);
        sb.append('}');
        return sb.toString();
    }

    //三个业务方法
    public void addEmp(Employee employee){
        // boss.setEmployees(new Employee[0]);
        if(null==employees){
            System.out.println("sorry,您没有权限添加!");
            return;
        }
        //说明是有权限添加.
        //对数组进行扩容操作
        employees = Arrays.copyOf(employees,employees.length+1);
        employees[employees.length-1]=employee;
    }

    //输出所有的下属的信息
    public void findAll(){
        if(null!=employees && employees.length>0){
            for (Employee employee : employees) {
                System.out.println(employee);
            }
        }else{
            System.out.println("sorry,请先添加下属!");
        }
    }

    //根据id进行删除

    public void delById(int id){
        if(null!=employees && employees.length>0){
            //2. 判断下属id是否存在...
            //定义一个计数器
            int count = 0;
            for (Employee e : employees) {//下属数组
                if(e.getId() == id){
                    count++;
                    //因为id是唯一的,只需要找到一个即可.
                    break;//跳出当前循环...
                }
            }
            //判断是否存在
            if(count==0){
                System.out.println("sorry,没有找到相关下属!");
                return;//直接结束整个方法...
            }
            //id记录的下属在下属的数组中才是存在的....
            //增删 => 新建一个新的数组 => 必不可少的条件 => 1. 元素类型    2. 数组的长度.
            Employee[] emps = new Employee[employees.length-1];//因为id是唯一的.只能一次只能删除1个

            //下标计数器
            int pos = 0;
            //需要遍历原来的数组
            for (Employee e : employees) {
                if(e.getId()!=id){
                    //数组的下标必须是连续的
                    emps[pos++] = e;
                }
            }
            //System.out.println(emps.length);
            //employees => 指向的还是原来的数组
            //emps才是指向不包含id记录的新的数组
            employees = emps;

        }else{
            System.out.println("sorry,没有任何下属信息!");
        }
    }



    //开除薪资大于1500.0d
    public void delByPrice(double price){
        if(null!=employees && employees.length>0){
            //1. 统计是否有符合要求的员工,并且统计出个数
            //定义一个计数器
            int count = 0;
            for (Employee e : employees) {
                if(e.getSalary()>=price){
                    count++;
                }
            }
            //判断一下是否存在
            if(count==0){
                System.out.println("sorry,么有员工可以被删除!");
                return;//结束
            }

            //确定新的数组的长度
            //TODO... 必须是通过创建新数组的手段来呈现一种"数组删除"的现象
            Employee[] temps = new Employee[employees.length-count];
            //employees  1000 1500 1000 1200 1500 1000   => count=2
            //temps  e1
            int pos = 0;

            for (Employee employee : employees) {
                if(employee.getSalary()<price){

                    temps[pos++] = employee;
                }
            }
            //很核心....
            employees = temps;
        }else{
            System.out.println("sorry");
        }
    }
    }

